package com.example.turnoverwarning.controller;

import com.example.turnoverwarning.dto.PageResult;
import com.example.turnoverwarning.dto.Result;
import com.example.turnoverwarning.dto.ShopAlertRequest;
import com.example.turnoverwarning.entity.Shop;
import com.example.turnoverwarning.mapper.ShopMapper;
import com.example.turnoverwarning.service.ShopService;
import com.example.turnoverwarning.vo.ShopDetailVO;
import com.example.turnoverwarning.vo.ShopVO;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/api/shop-alerts")
@AllArgsConstructor
public class ShopAlertController {
    private final ShopService shopService;

    /**
     * 分页数据展示
     * @param page
     * @param size
     * @param name
     * @return
     */
    @GetMapping
    public PageResult<ShopVO> listShops(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String name
    ) {
        PageResult<ShopVO> result = shopService.listShops(page, size, name);
        return result;
    }

    /**
     * 店铺详情
     * @param shopId
     * @param withOrders 否携带当日订单
     * @return
     */
    @GetMapping("/{shopId}")
    public ResponseEntity<Result> getShopDetail(
            @PathVariable Long shopId,
            @RequestParam(required = false) Boolean withOrders // 是否携带当日订单
    ) {
        ShopDetailVO shopDetail = shopService.getShopDetail(shopId, withOrders);
        return ResponseEntity.ok(Result.success("shopDetail"));
    }

    /**
     * 新增预警信息
     * @param request
     * @return
     */
    @PostMapping("/add")
    public ResponseEntity<Result> createAlert(
            @RequestBody ShopAlertRequest request
    ) {

        shopService.addShopAlert(request.getId(),
                request.getMail(),
                request.getThreshold());
        return ResponseEntity.ok(Result.success("预警设置成功"));
    }


    /**
     * 更新预警信息
     * @param shopId
     * @param request
     * @return
     */
    @PostMapping("/update/{shopId}")
    public ResponseEntity<Result> updateShopAlert(
            @PathVariable Long shopId,
             @RequestBody ShopAlertRequest request
    ) {
        Shop updatedShop = shopService.updateShopAlert(shopId, request.getMail(), request.getThreshold());
        return ResponseEntity.ok(Result.success("预警更新成功"));
    }

    /**
     * 清除预警信息
     * @param shopId
     * @return
     */
    @PostMapping("/clear/{shopId}")
    public ResponseEntity<Result> clearAlertConfig(
            @PathVariable Long shopId
    ) {
        Shop updatedShop = shopService.clearAlertConfig(shopId);
        return ResponseEntity.ok(
                Result.success("预警配置已清除")
        );
    }

    // 统一异常处理
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<Result> handleValidationException(MethodArgumentNotValidException ex) {
        String errorMsg = ex.getBindingResult().getAllErrors().get(0).getDefaultMessage();
        return ResponseEntity.badRequest().body(Result.fail("参数校验失败: " + errorMsg));
    }
}
